/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 4/23/13
 * Time: 3:10 PM
 * To change this template use File | Settings | File Templates.
 */
var MyPage = Class.create({
    initialize: function(o){
        //o {id, name book->MyBook, }

        this.id = o.id;
        this.book = o.book;

        this.divTag = new Element("div", {id: o.id, class: "page-view"});
        $j("#" + Static.containerView).append(this.divTag);
        //Xu ly truong hop khi nguoi dung khong click vao bat ky element nao
        $j(this.divTag).on("click", function(e){

            if(Global.currentSelectedEle != null){
                $j("#" + Global.currentSelectedEle).removeClass("resource-selected");
                Global.currentSelectedEle = null;
                Global.elementsView.reload();
                Global.propertyEditor.clear();
            }

        });

        this.elements = new Array();
        this.elementIdFeed = 0;
    },
    destroy: function(){
        $j(this.divTag).remove();
    },
    getNewElementId: function(){
        return this.id+"-ele"+this.elementIdFeed++;
    },
    addElement: function(e, autoAssignNewId){
        autoAssignNewId = autoAssignNewId !== undefined ? autoAssignNewId : true;

        Global.undoRedo.pushUndoStack("ADD_ELEMENT", this.toJSON());
        if(autoAssignNewId == true){
            var newIdForElement;
            do{
                newIdForElement = this.getNewElementId();
            }while($j("#"+newIdForElement).length>0);

            e.setValueByKey("id", newIdForElement);
        }

        this.elements.push(e);

        //Reload lai elementsView
        Global.elementsView.reload();
        MyTools.viewThumbnail(this.id);
    },
    removeElement: function(e){
        //Xoa element o day
        this.removeElementById(e.id);


    },
    removeElementById: function(id){
        for(var i = 0; i < this.elements.length; i++){
            if(this.elements[i].id == id){
                Global.undoRedo.pushUndoStack("REMOVE_ELEMENT", this.toJSON());

                $j("#" + id).remove();
                this.elements[i] = null;
                break;
            }
        }

        this.elements = this.elements.compact();

        Global.elementsView.reload();
        Global.propertyEditor.clear();
        MyTools.viewThumbnail(this.id);
    },
    getElementById: function(id){
        for(var i = 0; i< this.elements.length;i++){
            if(this.elements[i].id == id){
                return this.elements[i];
            }
        }

        return null;
    },
    toJSON: function(){
        var page = new Object();
        page['id'] = this.id;
        page['elementIdFeed'] = this.elementIdFeed;
        page['elements'] = new Array();
        $j.each(this.elements, function(index, value){
            page['elements'].push(value.toJSON());
        });

        return page;
    },
    fromJSON: function(o){
        this.id = o.id;
        $j(this.divTag).empty();
        this.elements.clear();
        this.elementIdFeed = o.elementIdFeed;

        var _this = this;

        $j.each(o.elements, function(index, value){
            var newEle = new window[value.className]({parent:_this});
            newEle.initAttributeList(value);
            newEle.initEvents();
            _this.elements.push(newEle);
        });
    },
    setActive: function(o){
        if(o == true){
            $j("#" + Static.containerView).find(".page-view").css("display", "none");
            $j(this.divTag).css("display", "block");
            $j("#" + Static.containerPagePreview).find("li.selected").removeClass("selected");
            $j("#" + Static.containerPagePreview).find("li[pageId=" + this.id + "]").addClass("selected");

            Global.currentPage = this;
            Global.elementsView.load(this);
        }
    }
});